/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Model.Data.Type
{
    public class ObjectRef:IEquatable<ObjectRef>
    {
        #region internal Attributes

        private String groupIdentifier;

        public String GroupIdentifier
        {
            get { return groupIdentifier; }
            set {

                if ( value.StartsWith( "~" ) )
                    groupIdentifier = value.Remove( 0, 1 );
                else
                    groupIdentifier = value; 
                }
        }
        private int objectNumber;

        public int ObjectNumber
        {
            get { return objectNumber; }
            set { objectNumber = value; }
        }

        #endregion

        #region Constructors

        public ObjectRef () { }

        public ObjectRef ( String groupIdentifier, int objectNumber )
        {
            this.GroupIdentifier = groupIdentifier;
            this.ObjectNumber = objectNumber;
        }

        public ObjectRef ( ObjectRef from )
        {
            this.GroupIdentifier = from.groupIdentifier;
            this.ObjectNumber = from.objectNumber;
        }

        #endregion

        #region Actions

        public override bool Equals ( Object o )
        {
            if ( this == o ) return true;
            ///if ( !( o is ObjectRef ) ) return false;

            ObjectRef objectRef = o as ObjectRef;

            return ( o!=null && ( ObjectNumber == objectRef.ObjectNumber )&&
                    ( GroupIdentifier == objectRef.GroupIdentifier ) );
            
            //if ( objectNumber != objectRef.objectNumber ) return false;
            
            //if ( groupIdentifier != null ? !groupIdentifier.Equals( objectRef.groupIdentifier ) :
            //                            objectRef.groupIdentifier != null ) return false;

            //return true;
        }

        public override int GetHashCode ()
        {
            return ToString().GetHashCode();
        }

        public override String ToString ()
        {
        //    if ( groupIdentifier.StartsWith( "~" ) )
         //       return groupIdentifier.Remove( 0, 1 ) + ":" + objectNumber;
          //  else
                return groupIdentifier + ":" + objectNumber;
        }

        #endregion

        #region IEquatable<ObjectRef> Members

        public bool Equals ( ObjectRef other )
        {
            return ( other!=null && ( ObjectNumber == other.ObjectNumber )&&
                    ( GroupIdentifier == other.GroupIdentifier ) );

            //return ( this.GetHashCode() == other.GetHashCode() );
        }

        #endregion


    }
}
